import {
	dbName,
	Social
} from '@/libs/social.sqlite.js';

const tableName = "OwnedObject";

const OwnedObject = {
	init: function() {
		let tableStructure =
			`
			id INTEGER PRIMARY KEY AUTOINCREMENT,
			dungeonId INTEGER NOT NULL,
			scriptId INTEGER NOT NULL,
			chapterId INTEGER NOT NULL,
			stageId INTEGER NOT NULL,
			melodramaId INTEGER NOT NULL,
			groupId INTEGER NOT NULL,
			name VARCHAR(80) NOT NULL,
			medium VARCHAR(10) NOT NULL,
			source_md5 VARCHAR(10) NOT NULL,
			source_remote_url TEXT NOT NULL,
			source_local_path TEXT NOT NULL,
			usable Booean NOT NULL,
			isPrivate Booean NOT NULL,
			shareable Booean NOT NULL,
			mustBePublished Booean NOT NULL,
			ownedQuantity TINYINT NOT NULL,
			usedQuantity TINYINT NOT NULL,
			isNew Booean NOT NULL
			`;
		plus.sqlite.executeSql({
			name: dbName,
			sql: `create table if not exists ${tableName}(${tableStructure})`,
			success(e) {
				console.log("Succeed to executeSql");
			},
			fail(e) {
				console.log('Failed to executeSql: ' + JSON.stringify(e))
			}
		});
	},
	add: function(dungeonId,scriptId,chapterId,stageId,melodramaId,groupId,name,medium,
			source_md5,source_remote_url,source_local_path,usable,isPrivate,shareable,mustBePublished,ownedQuantity) {
		let insertSql =
			`insert into ${tableName}(dungeonId,scriptId,chapterId,stageId,melodramaId,groupId,name,medium,
			source_md5,source_remote_url,source_local_path,usable,isPrivate,shareable,mustBePublished,ownedQuantity,usedQuantity) 
			values ("${dungeonId}","${scriptId}","${chapterId}","${stageId}","${melodramaId}","${groupId}","${name}","${medium}"
			,"${source_md5}","${source_remote_url}","${source_local_path}","${usable}","${isPrivate}","${shareable}","${mustBePublished}","${ownedQuantity}",0,true)`;
		console.log("Prepare sql:" + insertSql);
		plus.sqlite.executeSql({
			name: dbName,
			sql: insertSql,
			success(e) {
				console.log("Succeed to executeSql");
			},
			fail(e) {
				console.log('Failed to executeSql: ' + JSON.stringify(e))
			}
		});
	},
	listByChapters: function(chapterIds, callback) {
		plus.sqlite.selectSql({
			name: dbName,
			sql: `select * from ${tableName} where chapterId in (${chapterIds})`,
			success(data) {
				console.log("Succeed to selectSql");
				callback(data);
			},
			fail(e) {
				console.log('Failed to selectSql: ' + JSON.stringify(e))
			}
		});
	},
	listByGroups: function(groupIds, callback) {
		plus.sqlite.selectSql({
			name: dbName,
			sql: `select * from ${tableName} where groupId in ("${groupIds}")`,
			success(data) {
				console.log("Succeed to selectSql");
				callback(data);
			},
			fail(e) {
				console.log('Failed to selectSql: ' + JSON.stringify(e))
			}
		});
	},
	check: function(objectId) {
		let updateSql = `update ${tableName} set isNew=false where id = ${objectId}`;
		plus.sqlite.executeSql({
			name: dbName,
			sql: updateSql,
			success(e) {
				console.log("Succeed to executeSql");
			},
			fail(e) {
				console.log('Failed to executeSql: ' + JSON.stringify(e))
			}
		});
	},
}

export {
	OwnedObject
}